<?php
class JailCell extends BoardCell{
	private $in_jail = array();
	
	public function __construct($config)
	{
		parent::__construct($config);
		
	} 
	
	/**
	 * Places a player in Jail
	 * 
	 * @param object $player
	 * 
	 * @return void 
	 */
	public function jailPlayer($player) {
		$player->setNumberOfDoubles(0);
		$this->in_jail[] = array(
							'player' 		=> $player,
							'roundsInJail'	=> 0);
	}
	
	/**
	 * Returns if a player is found in jail
	 * 
	 * @param object $player
	 * 
	 * @return boolean $jailed 
	 */
	public function inJail($player) {
		$jailed = false;
		foreach ($this->in_jail as $key=>$jailed_player) {
			if($player == $this->in_jail[$key]['player']) {
				$jailed = true;	
			}
		}
		return $jailed;
	}
	
	/**
	 * Release the player from jail
	 * 
	 * @param object $player
	 * 
	 * @return void 
	 */
	public function releasePlayer($player) {
		foreach ($this->in_jail as $key=>$jailed_player) {
			if($player == $jailed_player['player']) {
				unset($this->in_jail[$key]);
			}
		}
		if(count($this->in_jail) > 1 )
		{
			//clean up the array keys
			$this->in_jail = array_values($this->in_jail);
		}
	}
	
	public function getJailedPlayers()
	{
		$players = array();
		foreach ($this->in_jail as $key=>$jailed_player) 
		{
			$players[] = $jailed_player['player'];		
		}
		return $players;
	}
	
	public function increasePlayersRoundsInJail($player)
	{
		foreach ($this->in_jail as $key=>$jailed_player) {
			if($player == $jailed_player['player']){
				$this->in_jail[$key]['roundsInJail'] += 1;
			}
		}
	}
	
	public function getPlayersRoundsInJail($player)
	{
		$value = 0;
		foreach ($this->in_jail as $key=>$jailed_player) {
			if($player == $jailed_player['player']) {
				$value = $this->in_jail[$key]['roundsInJail'];
			}
		}
		return $value;
	}
	
}